/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.file.service.file.impl;

import java.util.Date;
import java.util.Optional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.hyts.infodb.enums.FileTypeEnum;
import com.hyts.infodb.enums.StatusTypeEnum;
import com.hyts.infodb.error.subclass.DaoException;
import com.hyts.infodb.file.bean.file.FileBO;
import com.hyts.infodb.file.bean.file.FilePO;
import com.hyts.infodb.file.bean.file.FileVO;
import com.hyts.infodb.file.dao.file.FileDAO;
import com.hyts.infodb.file.service.file.FileService;

/** 
 * @author LiBo/Alex
 * @see 
 * com.hyts.infodb.file.service.file.FileService
 * com.hyts.infodb.file.bean.FileBO
 * @since V1.0
 * @version V1.0 
 */
@Service
public class FileServiceImpl implements FileService {
        
    
    @Autowired
    private FileDAO fileDao;
    
    /* 
     * @param file
     * @return
     * @see com.hyts.infodb.file.service.FileService#findFileList(com.hyts.infodb.file.bean.FileBO)  
     * @exception
     */

    @Override
    public FileVO findFileList(FileBO file) {
        FileVO vo = new FileVO();
        Page<FilePO> pageData = fileDao.findAll(file.buildPageData(file.getPageNum(), file.getPageSize()));
        vo.setContents(pageData.getContent());
        vo.setTotalDataSize(pageData.getTotalElements());
        vo.setTotalPageNumber(pageData.getTotalPages());
        return  vo;
    }

    /* 
     * @param file
     * @return
     * @see com.hyts.infodb.file.service.FileService#loadFile(com.hyts.infodb.file.bean.FileBO)  
     * @exception
     */
    @Override
    public FileVO loadFile(FileBO file) {
        FileVO vo = new FileVO();
        Optional<FilePO> fileObj = fileDao.findById(file.getId());
        if(fileObj.isPresent()) {
            vo.setContent(fileDao.findById(file.getId()).get());
        }
        return vo;
    }

    /* (非 Javadoc)
     * @param file
     * @return
     * @see com.hyts.infodb.file.service.FileService#saveFile(com.hyts.infodb.file.bean.FileBO)  
     * @exception
     */
    @Override
    public boolean saveFile(FileBO file) {
        FilePO filePO = new FilePO();
        BeanUtils.copyProperties(file, filePO);
        filePO.setFileType(FileTypeEnum.match(file.getFileType()));
        if(filePO.getCreateDate() == null)
            filePO.setCreateDate(new Date());
        if(filePO.getIsHidden() == null)
            filePO.setIsHidden(StatusTypeEnum.SHOW.getValue());
        if(filePO.getIsRemote() == null)
            filePO.setIsRemote((byte) StatusTypeEnum.LOCAL.getValue().intValue());
        filePO.setEditDate(new Date());
        return Optional.ofNullable(fileDao.save(filePO)).isPresent();
    }

    /* (非 Javadoc)
     * @param file
     * @return
     * @see com.hyts.infodb.file.service.FileService#deleteFile(com.hyts.infodb.file.bean.FileBO)  
     * @exception
     */

    @Override
    public boolean deleteFile(FileBO file) {
        FilePO filePO = new FilePO();
        BeanUtils.copyProperties(file, filePO);
        //filePO.setFileType(FileTypeEnum.match(file.getFileType()));
        try {
            fileDao.delete(filePO);
        } catch (Exception e) {
            throw new DaoException("删除文件失败!",e);
        }
        return true;
    }

}
